﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Windows.Forms;
using TCommon.Util;

namespace TCommon
{
    public class ServiceManager
    {
        public int ChangeUserName(string ownId, string userName) {
            var request = new ChangeNameRequest()
            {
                OwnId = ownId,
                UserName = userName
            };

            var response = (ChangeNameResponse)HttpUtil.SendRequest<ChangeNameRequest>("https://www.1meeting.cn/modifyname",
                request, typeof(ChangeNameResponse));

            return response == null ? 0 : response.Code;
        }

        public int SetFocuseUser(string ownId, string userName) {
            var request = new FocuseUserRequest()
            {
                OwnId = ownId,
                FocuseId = userName,
                IsShare = TCommon.AppContext.isSharingScreen? 0 : 1,
                IsSilent = TCommon.AppContext.AllowChat? 0 : 1,
                IsMike = TCommon.AppContext.AllowOnMic? 0 : 1,
            };

            var response = (FocuseUserResponse)HttpUtil.SendRequest<FocuseUserRequest>("https://www.1meeting.cn/addfocus",
                request, typeof(FocuseUserResponse));

            return response == null ? 0 : response.Code;
        }

        public string GetFocuseUser(string ownId) {
            var request = new GetFocuseRequest()
            {
                OwnId = ownId
            };

            var response = (GetFocuseResponse)HttpUtil.SendRequest<GetFocuseRequest>("https://www.1meeting.cn/findfocus",
                request, typeof(GetFocuseResponse));

            if (response != null && response.Code == 0) {
                return response.FocuseId;
            }

            return null;
        }

        public string GetUserSign(int roomId, string userName) {

            string userSign = string.Empty;
            var param = new Request()
            {

            };

            string urlFormat = "https://www.1meeting.cn/getusersig?roomid={0}&userid={1}";
            var res = (UserSign)HttpUtil.SendRequest<Request>(string.Format(urlFormat, roomId, userName),
            param, typeof(UserSign));

            if (res != null)
                userSign = res.Sign;

            return userSign;
        }

        public Response GetMeetingNumber(string macAddress, string meetingID, out string ownId)
        {
            Response result = null;
            ownId = string.Empty;
            try
            {
                var urlFormat = "https://www.1meeting.cn/txvideoconferencing?networkId={0}&wifiId={1}";
                //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format(urlFormat, macAddress, macAddress));
                var param = new Request()
                {
                    MachineID = macAddress,
                    ID = meetingID,
                    NetworkID = macAddress,
                    WifiID = macAddress
                };

                result = (Response)HttpUtil.SendRequest<Request>(string.Format(urlFormat, macAddress, macAddress),
                param, typeof(Response));
                //DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Request));
                //var stream = new MemoryStream();
                //serializer.WriteObject(stream, param);
                //byte[] dataBytes = new byte[stream.Length];
                //stream.Position = 0;
                //stream.Read(dataBytes, 0, (int)stream.Length);
                //string dataString = Encoding.UTF8.GetString(dataBytes);
                //stream.Close();
                //request.Method = "POST";
                //request.ContentType = "application/json";

                //var requestByte = Encoding.UTF8.GetBytes(dataString);
                //request.ContentLength = requestByte.Length;
                //using (var requestStream = request.GetRequestStream())
                //{
                //    requestStream.Write(requestByte, 0, requestByte.Length);
                //    var response = (HttpWebResponse)request.GetResponse();
                //    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                //    var returnString = sr.ReadToEnd();
                //    var responseStream = new MemoryStream(Encoding.UTF8.GetBytes(returnString));
                //    DataContractJsonSerializer seri = new DataContractJsonSerializer(typeof(Response));
                //    var res = (Response)seri.ReadObject(responseStream);
                //    result = res;
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取会议信息失败,请点击刷新按钮重试");
            }

            return result;
        }

        public JoinMeetingResponse JoinMeeting(string rooId) {
            var urlFormat = "https://www.1meeting.cn/joinmeeting?roomid={0}";
            var param = new Request()
            {

            };

            var res = (JoinMeetingResponse)HttpUtil.SendRequest<Request>(string.Format(urlFormat, rooId),
                param, typeof(JoinMeetingResponse));

            return res;
        }
    }

    #region
    [DataContract]
    public class JoinMeetingResponse
    {
        [DataMember(Name = "code")]
        public int Code { get; set; }

        [DataMember(Name = "hostname")]
        public string HostId { get; set; }

        [DataMember(Name = "count")]
        public int Count { get; set; }

        [DataMember(Name = "scene")]
        public string Scene { get; set; }
    }

    [DataContract]
    public class UserSign
    {
        [DataMember(Name = "usersig")]
        public string Sign { get; set; }
    }

    [DataContract]
    public class Response
    {
        [DataMember(Name = "code")]
        public long Code { get; set; }

        [DataMember(Name = "hostid")]
        public string HostId { get; set; }

        [DataMember(Name = "ownid")]
        public ulong OwnId { get; set; }

        [DataMember(Name = "rtmpurl")]
        public string RtmpUrl { get; set; }

        [DataMember(Name = "windowversion")]
        public string WindowsVersion { get; set; }

        [DataMember(Name = "windowsurl")]
        public string DownloadUrl { get; set; }
    }

    [DataContract]
    public class Request
    {
        [DataMember(Name = "machineId")]
        public string MachineID { get; set; }

        [DataMember(Name = "id")]
        public string ID { get; set; }

        [DataMember(Name = "wifiId")]
        public string WifiID { get; set; }

        [DataMember(Name = "networkId")]
        public string NetworkID { get; set; }
    }

    [DataContract]
    public class ChangeNameRequest {
        [DataMember(Name = "ownid")]
        public string OwnId { get; set; }

        [DataMember(Name = "name")]
        public string UserName { get; set; }
    }

    [DataContract]
    public class ChangeNameResponse
    {
        [DataMember(Name = "code")]
        public int Code { get; set; }
    }

    [DataContract]
    public class FocuseUserRequest
    {
        [DataMember(Name = "ownid")]
        public string OwnId { get; set; }

        [DataMember(Name = "focusid")]
        public string FocuseId { get; set; }

        [DataMember(Name = "is_share")]
        public int IsShare { get; set; }

        [DataMember(Name = "is_silent")]
        public int IsSilent { get; set; }

        [DataMember(Name = "is_mike")]
        public int IsMike { get; set; }

        [DataMember(Name = "is_speak")]
        public int IsSpeak { get; set; }
    }

    [DataContract]
    public class FocuseUserResponse
    {
        [DataMember(Name = "code")]
        public int Code { get; set; }
    }

    [DataContract]
    public class GetFocuseRequest
    {
        [DataMember(Name = "ownid")]
        public string OwnId { get; set; }
    }

    [DataContract]
    public class GetFocuseResponse
    {
        [DataMember(Name = "code")]
        public int Code { get; set; }

        [DataMember(Name = "focusid")]
        public string FocuseId { get; set; }
    }
    #endregion
}
